<template>
  <el-dialog class="formdrawer" :title="'预览备料单'" v-model="visible">
    <div style="display: flex; justify-content: flex-end">
      <el-button type="primary" @click="printBtn">
        {{ $t("entirety.print") }}
      </el-button>
    </div>

    <!--  表单内容 -->
    <el-form
      class="formstyle ToPrint"
      :model="form"
      ref="dataFormRef"
      :rules="rules"
      label-position="right"
      label-width="120px"
      @submit.prevent
    >
    </el-form>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <div>预览备料单内容</div>
    <!-- 底部的确认、取消按钮 -->
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancleBtn">{{ "关闭" }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
// import {
//   apiAddUser,
//   apiPutUserInfo,
//   apiGetUser,
//   apiAllRoleUser,
// } from "@/apis/user";
// import emitter from "@/utils/eventbus"; //引入bus

// 定义变量内容
const visible = ref(false);
const loading = ref(false);
const dataFormRef = ref(); // form表单的ref
const roleArr = ref(); //角色数组

// 提交表单数据
const form = reactive({
  userId: "",
  roleId: 1,
  userNo: "",
  userPwd: "123",
  realName: "",
  telephone: "",
  userStatus: "1",
  userSex: "1",
  userAge: "",
});
// 清空表单
const clearForm = () => {
  form.userId = "";
  form.roleId = 1;
  form.userNo = "";
  form.userPwd = "123";
  form.realName = "";
  form.telephone = "";
  form.userStatus = "1";
  form.userSex = "1";
  form.userAge = "";
};
// 校验规则
const rules = {
  userNo: [
    {
      required: true,
      message: "工号不可为空",
    },
  ],
  realName: [
    {
      required: true,
      message: "姓名不可为空",
    },
    {
      min: 2,
      max: 5,
      message: "姓名长度必须在2到5之间",
    },
  ],
  telephone: [
    {
      required: true,
      message: "电话不可为空",
    },
    {
      pattern: /^[1][3,4,5,7,8,9][0-9]{9}$/,
      message: "电话格式不正确",
    },
  ],
  userPwd: [
    {
      required: true,
      message: "密码不可为空",
    },
  ],
  userSex: [
    {
      required: true,
      message: "性别不可为空",
    },
  ],
  userAge: [
    {
      required: true,
      message: "年龄不可为空",
    },
  ],
  roleId: [
    {
      required: true,
      message: "角色不可为空",
    },
  ],
  userStatus: [
    {
      required: true,
      message: "状态不可为空",
    },
  ],
};

// 打开弹窗
const openDialog = (tempId: string) => {
  visible.value = true; // 打开弹窗
  form.userId = ""; // 防止上次的操作还在表单上，影响本次操作
  // 重置表单数据---否则新增弹窗会自动触发校验
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取信息
  if (tempId) {
    // 如果有id，就编辑逻辑
    form.userId = tempId;
    getElevatorInfoData(tempId); // 初始化表单数据的方法
  } else {
    // 添加逻辑
    clearForm(); //清空表单
  }
};
// 编辑：初始化表单数据
const getElevatorInfoData = (tempId: string) => {
  loading.value = true;
  // apiGetUser(tempId) // 编辑回显接口
  //   .then((res: any) => {
  //     Object.assign(form, res.data); // 将接口数据赋值到本地
  //   })
  //   .finally(() => {
  //     loading.value = false;
  //   });
};

// 取消按钮
const cancleBtn = () => {
  visible.value = false;
};

const printBtn = () => {
  // 打印当前内容
  window.print();
};
// 暴露变量
defineExpose({
  openDialog,
});
</script>

<style scoped lang="scss">
.formstyle {
  padding: 0 80px;
}

@media print {
  .ToPrint {
    visibility: visible;
    position: absolute;
    top: 0;
    left: 0;
  }
}
</style>
